package com.pilipili.zeus.educms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pilipili.zeus.educms.entity.CrmBanner;
import com.pilipili.zeus.educms.entity.vo.BannerQuery;
import com.pilipili.zeus.educms.mapper.CrmBannerMapper;
import com.pilipili.zeus.educms.service.CrmBannerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 首页banner表 服务实现类
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-30
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {
    @Override
    public void pageQuery(Page<CrmBanner> pageParam, BannerQuery query) {

        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        if (query == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        String id = query.getId();
        String title = query.getTitle();

        if (!StringUtils.isEmpty(id)) {
            queryWrapper.like("id", id);
        }

        if (!StringUtils.isEmpty(title) ) {
            queryWrapper.eq("title", title);
        }
        queryWrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, queryWrapper);
    }
    //双引号里可以是#形参名，单引号就是纯字符串
    @Cacheable(key="'listBanner'",value ="banner" )
    @Override
    public List<CrmBanner> listBanner() {
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_modified");
        queryWrapper.last("limit 8");
        return baseMapper.selectList(queryWrapper);
    }

}
